package com.bawei.bydayzg_two.fragment

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.bawei.base.mvvm.BaseViewFragment
import com.bawei.bydayzg_two.R
import com.bawei.bydayzg_two.Utils.GildUtils
import com.bawei.bydayzg_two.databinding.FragmentMineBinding
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector

class MineFragment : BaseViewFragment<FragmentMineBinding>() {
    override fun getLoutyId(): Int {
        return R.layout.fragment_mine
    }

    override fun initView() {

        val navHostFragment =
            childFragmentManager.findFragmentById(R.id.fragmentContainerView2) as NavHostFragment
        //
        val navController = navHostFragment.navController
        //
        binding.bnv.setupWithNavController(navController)

        //第二种方法
//        binding.bnv.setupWithNavController(requireActivity().findNavController(R.id.fragmentContainerView2))

        //性别选择器
        binding.userSex.setOnClickListener {
            val list = mutableListOf("男", "女", "外星人")
            //
            val optionPicker = OptionPicker(requireActivity())
            optionPicker.setTitle("请选择性别")
            optionPicker.setData( list)
            optionPicker.setOnOptionPickedListener { position, item ->
                binding.userSex.text = list[position]
            }
            //
            optionPicker.show()
        }

        //日期弹出框
        binding.userTime.setOnClickListener {
            val datePicker = DatePicker(requireActivity())
            datePicker.wheelLayout.setRange(DateEntity.target(1900,1,1),
                DateEntity.today(),
                DateEntity.today()
                )
            //
            datePicker.setOnDatePickedListener { year, month, day ->
                binding.userTime.text = "$year-$month-$day"
            }
            //
            datePicker.show()
        }

        //地址弹出框
        binding.userAddress.setOnClickListener {
            val addressPicker = AddressPicker(requireActivity())
            addressPicker.setTitle("请选择地址")
            addressPicker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY)
            addressPicker.setOnAddressPickedListener { province, city, county ->
                binding.userAddress.text = "${province.name} ${city.name} ${county.name}"
            }
            //
            addressPicker.show()
        }


        //
        binding.userIv.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }
    }

    override fun initDate() {

    }
    //
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        //
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == Activity.RESULT_OK){
            val pictureBean = data!!.extras!!.get(PictureSelector.PICTURE_RESULT) as PictureBean
            //
            context?.let { GildUtils.loadYuan(it,pictureBean.path,binding.userIv) }
        }
    }


}